//
//  CacheHelper.m
//  HdStore
//
//  Created by zhenqiu on 15/11/23.
//  Copyright (c) 2015年 zhenqiu. All rights reserved.
//

#import "CacheHelper.h"


@interface CacheHelper()

-(id) init;
-(void)setUserDefaultValue:(id)value forKey:(id)key;
-(id)getUserDefaultValue:(id)key;

@end

@implementation CacheHelper

@synthesize username;
@synthesize passward;


+ (id) getInstance
{
    static dispatch_once_t once ;
    static id instance = nil;
    
    dispatch_once(&once, ^{
        
        instance = [[CacheHelper alloc]init];
        
    });
    
    return instance;
    
}

-(instancetype) init{
    
    self = [super init];
    if(self)
    {
        
    }
    
    return self;
}


-(void)setCacheValue:(id)value forKey:(NSString *)key{
    
    [self setUserDefaultValue:value forKey:key];
}

-(id)getCacheValue:(NSString *)key{
    
    return [self getUserDefaultValue:key];
}

-(id)getUserId{
    
  return [self getCacheValue: USER_ID_KEY];
    
}
-(id)getPhoneNumber {
    return [self getCacheValue:USER_PHONE_KEY];
}

-(id)getUserAvatar{
    
    return [self getCacheValue:USER_AVATAR_KEY];
}

-(void)cacheUserId:(id) userid{
    
    [self setCacheValue:userid forKey:USER_ID_KEY];
}
-(void)cachePhoneNumber:(id)phoneNumber {
    [self setCacheValue:phoneNumber forKey:USER_PHONE_KEY];
}

-(void)cacheUserAvatar:(id)avatarUrl{
    
    [self setCacheValue:avatarUrl forKey:USER_AVATAR_KEY];
}

-(id)getCityInfo{
    
    return [self getCacheValue:CITY_INFO_KEY];
}


-(void)cacheCityInfo:(id) cityInfo{
    
    [self setCacheValue:cityInfo forKey:CITY_INFO_KEY];
}

-(void)cacheUserInputInfo:(id)dic withKey:(NSString*)key{
    
    if(!dic){
        
        return;
    }
    
    id cacheDic  = [self getCacheValue:key];
    
    if(!cacheDic){
        
        [self setCacheValue:dic forKey:key];
    }
    
    NSMutableDictionary * mDic = [NSMutableDictionary dictionaryWithDictionary:cacheDic];
    
    for (NSInteger i =0 ; i < [[dic allKeys] count]; i ++) {
        NSString * key = [[dic allKeys] objectAtIndex:i];
        
        [mDic setValue:[dic valueForKey:key] forKey:key];
    }
    
    [self setCacheValue:mDic forKey:key];
}


#pragma mark -
#pragma mark userdefault 


-(void)setUserDefaultValue:(id)value forKey:(id)key{
    
    [[NSUserDefaults standardUserDefaults] setValue:value forKey:key];
}
-(id)getUserDefaultValue:(id)key{
    
   return  [[NSUserDefaults standardUserDefaults] valueForKey:key];
}


-(void)exit{
    
    NSMutableArray * keyArray = [NSMutableArray arrayWithObjects:USER_ID_KEY,USER_PHONE_KEY,USER_AVATAR_KEY,CREDIT_PERSONAL_INFORMATION_INFO,CREDIT_CONSUMER_INFORMATION_INFO,CREDIT_PICTURE_MATERIAL_INFO,CREDIT_CREDIT_SCORE_INFO,ME_LOAN_MYLOAN_INFO,ME_CREDIT_CARD_APPLY_RECORD_INFO,ME_BILL_DETAIL_HAND_INFO,ME_BILL_DETAIL_SPIDER_INFO,ME_BILL_CREDIT_CARD_MYLIST_INFO,ME_BILL_LIST_SPIDER_PAST_INFO, nil];
    for (NSInteger i = 0; i < [keyArray count]; i++) {
        
        NSString * key = [keyArray objectAtIndex:i];
        [self setCacheValue:nil forKey:key];
    }
    GYNLog(@"~~~~%@",[self getUserAvatar]);
    
}

@end
